package com.leoman.spinningmachine.tab3;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.leoman.helper.util.FrescoUtil;
import com.leoman.spinningmachine.R;
import com.leoman.spinningmachine.bean.LeaveBean;
import com.leoman.spinningmachine.bean.LeaveGroup;
import com.leoman.spinningmachine.constant.Constant;
import com.leoman.spinningmachine.util.DateUtil;
import com.leoman.spinningmachine.util.SharedPreferencesUtils;

import java.util.List;

/**
 * Created by Administrator on 2018-05-25.
 */

public class LeaveExpandableListViewAdapter extends BaseExpandableListAdapter {
    private Context mContext;
    private List<LeaveGroup> mData;
    private LayoutInflater inflate;

    public LeaveExpandableListViewAdapter(Context context, List<LeaveGroup> data) {
        this.mContext = context;
        this.mData = data;
        this.inflate = LayoutInflater.from(mContext);
    }

    @Override
    public int getGroupCount() {
        return mData == null ? 0 : mData.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        List<LeaveBean> lists = mData.get(groupPosition).getList();
        return lists == null ? 0 : lists.size();
    }

    @Override
    public LeaveGroup getGroup(int groupPosition) {
        return mData.get(groupPosition);
    }

    @Override
    public LeaveBean getChild(int groupPosition, int childPosition) {
        return mData.get(groupPosition).getList().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupHolder holder = null;
        if (convertView == null) {
            convertView = inflate.inflate(R.layout.item_leave_head, null);
            holder = new GroupHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (GroupHolder) convertView.getTag();
        }
        LeaveGroup bean = mData.get(groupPosition);
        String date = bean.getDate();
        holder.tv_month.setText(date.substring(date.length() - 2, date.length()) + "月");
        holder.tv_count.setText("(" + bean.getList().size() + ")");
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildHolder holder = null;
        if (convertView == null) {
            convertView = inflate.inflate(R.layout.item_leave, null);
            holder = new ChildHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ChildHolder) convertView.getTag();
        }
        LeaveBean bean = mData.get(groupPosition).getList().get(childPosition);
        holder.tv_leave_time.setText("" + DateUtil.getyMdStr(bean.getCreateDate()));
        holder.tv_leave_title.setText("" + bean.getReason());
        switch (bean.getTypeStatus()) {
            case 1:
                holder.leave_type.setText("请假类型:出差");
                break;
            case 2:
                holder.leave_type.setText("请假类型:事假");
                break;
            case 3:
                holder.leave_type.setText("请假类型:调休");
                break;
            case 4:
                holder.leave_type.setText("请假类型:病假");
                break;
            case 5:
                holder.leave_type.setText("请假类型:产假");
                break;
            case 6:
                holder.leave_type.setText("请假类型:陪产假");
                break;
            case 7:
                holder.leave_type.setText("请假类型:婚假");
                break;
            case 8:
                holder.leave_type.setText("请假类型:丧假");
                break;
            case 9:
                holder.leave_type.setText("请假类型:其他");
                break;
        }
        holder.leave_begin_time.setText("开始时间:" + DateUtil.getyMdHms(bean.getStartTime(), "yyyy-MM-dd HH:mm"));
        holder.leave_end_time.setText("结束时间:" + DateUtil.getyMdHms(bean.getEndTime(), "yyyy-MM-dd HH:mm"));
        holder.tv_sh_stage.setText("" + bean.getStatus());
        FrescoUtil.setImg(mContext, holder.iv_pic, SharedPreferencesUtils.getInstance().getString(Constant.HEADPATH));
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    class GroupHolder {

        public TextView tv_month;
        public TextView tv_count;

        public GroupHolder(View itemView) {
            tv_month = (TextView) itemView.findViewById(R.id.tv_month);
            tv_count = (TextView) itemView.findViewById(R.id.tv_count);
        }
    }

    class ChildHolder {
        public SimpleDraweeView iv_pic;
        public View view;
        public TextView tv_leave_title;
        public TextView tv_leave_time;
        public TextView leave_type;
        public TextView leave_begin_time;
        public TextView leave_end_time;
        public TextView tv_sh_stage;

        public ChildHolder(View itemView) {
            view = itemView.findViewById(R.id.view);
            iv_pic = (SimpleDraweeView) itemView.findViewById(R.id.iv_pic);
            tv_leave_title = (TextView) itemView.findViewById(R.id.tv_leave_title);
            tv_leave_time = (TextView) itemView.findViewById(R.id.tv_leave_time);
            leave_type = (TextView) itemView.findViewById(R.id.leave_type);
            leave_begin_time = (TextView) itemView.findViewById(R.id.leave_begin_time);
            leave_end_time = (TextView) itemView.findViewById(R.id.leave_end_time);
            tv_sh_stage = (TextView) itemView.findViewById(R.id.tv_sh_stage);
        }
    }
}
